﻿// -----------------------------
// Copyright © Kodi Studios 2012
// -----------------------------

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
using System.Collections.Generic;

namespace Kodi.Picturethrill
{
    public class DesktopBackground
    {
        private const string DesktopKeyPath = "Control Panel\\Desktop";

        public static void SetWallpaper(string fileName)
        {
            using (Log.Group(MethodInfo.GetCurrentMethod().Name + " " + fileName))
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    throw new ArgumentException("null or empty", "fileName");
                }

                if (!File.Exists(fileName))
                {
                    Log.Comment("File '" + fileName + "' doesn't exists");
                }

                Style style = Environment.OSVersion.Version >= Settings.Windows7Version ? Style.Filled : Style.Stretched;
                using (RegistryKey desktopKey = Registry.CurrentUser.OpenSubKey(DesktopKeyPath, true))
                {
                    switch (style)
                    {
                        case Style.Tiled:
                            desktopKey.SetValue(@"WallpaperStyle", "1");
                            desktopKey.SetValue(@"TileWallpaper", "1");
                            break;
                        case Style.Centered:
                            desktopKey.SetValue(@"WallpaperStyle", "1");
                            desktopKey.SetValue(@"TileWallpaper", "0");
                            break;
                        case Style.Stretched:
                            desktopKey.SetValue(@"WallpaperStyle", "2");
                            desktopKey.SetValue(@"TileWallpaper", "0");
                            break;
                        case Style.Fitted: // (Windows 7 and later)
                            desktopKey.SetValue(@"WallpaperStyle", "6");
                            desktopKey.SetValue(@"TileWallpaper", "0");
                            break;
                        case Style.Filled: // (Windows 7 and later)
                            desktopKey.SetValue(@"WallpaperStyle", "10");
                            desktopKey.SetValue(@"TileWallpaper", "0");
                            break;
                    }
                }

                // Path must be bitmap for SystemParametersInfo API
                // Windows 8 Pro max path is 255
                // Windows 8 Server max path is 255
                // Windows 7 max path is 259
                string bmpPath = Path.Combine(Settings.WallpaperBmpsPath, Path.GetFileName(fileName)) + ".bmp";
            
                try
                {
                    if (!File.Exists(bmpPath))
                    {
                        Image image = Image.FromFile(fileName);
                        image.Save(bmpPath, ImageFormat.Bmp);
                        Log.Comment("Bmp saved: " + bmpPath);
                    }
                    else
                    {
                        Log.Comment("Bmp already exists: " + bmpPath);
                    }

                    NativeMethods.SystemParametersInfo(NativeMethods.SPI_SETDESKWALLPAPER, 0, bmpPath, NativeMethods.SPIF_UPDATEINIFILE | NativeMethods.SPIF_SENDWININICHANGE);
                    Log.Comment("Bmp Applied to Desktop: " + bmpPath);
                }
                catch (Exception e)
                {
                    Log.Comment(e.ToString());
                }
            }
        }

        public static string GetWallpaperBmp()
        {
            using (RegistryKey desktopKey = Registry.CurrentUser.OpenSubKey(DesktopKeyPath, true))
            {
                return (string)desktopKey.GetValue("Wallpaper");
            }
        }

        private enum Style : int
        {
            Tiled, Centered, Stretched, Fitted, Filled
        }
    }
}
